package com.qkl.xiatang.app.web.controller;


import com.qkl.xiatang.app.dto.sale.BatchDetailDTO;
import com.qkl.xiatang.app.dto.sale.BatchWebDTO;
import com.qkl.xiatang.app.service.impl.SaleService;
import com.qkl.xiatang.app.utils.ErrorCodeUtils;
import com.qkl.xiatang.app.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("{app.baseURL}")
@Slf4j
public class SaleController {

    @Autowired
    private SaleService saleService;

    /**
     * 查询销售批次
     * @param params
     * @return
     */
    @GetMapping("/querySaleBatch")
    public Result querySaleBatch(@RequestParam Map<String,Object> params) {
        String pondId = StringUtils.trimToEmpty((String) params.get("pondId"));
        if(StringUtils.isEmpty(pondId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        return ControllerHelper.toQueryResult(saleService.querySaleBatch(pondId));
    }

    /**
     * 增加销售批次
     * @param params
     * @return
     */
    @PostMapping("/addSaleBatch")
    public Result addSaleBatch(@RequestBody  Map<String,Object> params) {
        String pondId = StringUtils.trimToEmpty((String) params.get("pondId"));
        if(StringUtils.isEmpty(pondId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        return ControllerHelper.toOperateResult(saleService.addSaleBatch(pondId));
    }

    /**
     * 保存批次明细
     * @param webdto
     * @return
     */
    @PostMapping("/saveSaleBatchDetail")
    public Result saveSaleBatchDetail(@RequestBody BatchDetailDTO webdto) {
        return ControllerHelper.toOperateResult(saleService.saveSaleBatchDetail(webdto));
    }

    /**
     * 查询批次明细列表
     * @param params
     * @return
     */
    @GetMapping("/querySaleBatchDetailList")
    public Result querySaleBatchDetailList(@RequestParam Map<String,Object> params) {
        String batchId= StringUtils.trimToEmpty((String) params.get("batchId"));
        if(StringUtils.isEmpty(batchId)){
            return Result.error(ErrorCodeUtils.PARAM_EMPTY, ErrorCodeUtils.PARAM_EMPTY_MSG);
        }
        return ControllerHelper.toQueryResult(saleService.querySaleBatchDetailList(batchId));
    }

    /**
     * 完成销售批次
     * @param webDTO
     * @return
     */
    @PostMapping("/finishSaleBatch")
    public Result finishSaleBatch(@RequestBody BatchWebDTO webDTO) {
        return ControllerHelper.toOperateResult(saleService.finishSaleBatch(webDTO));
    }

    /**
     * 删除销售批次
     * @param params
     * @return
     */
    @PostMapping("/deleteSaleBatch")
    public Result deleteSaleBatch(@RequestBody  Map<String,String> params) {
        String batchId = params.get("batchId");
        return ControllerHelper.toOperateResult(saleService.deleteSaleBatch(batchId));
    }

    /**
     * 删除销售批次明细
     * @param params
     * @return
     */
    @PostMapping("/deleteSaleBatchDetail")
    public Result deleteSaleBatchDetail(@RequestBody  Map<String,String> params) {
        String batchDetailId = params.get("batchDetailId");
        return ControllerHelper.toOperateResult(saleService.deleteSaleBatchDetail(batchDetailId));
    }

}
